package cn.iocoder.yudao.module.ym.controller.admin.ticket;

import cn.iocoder.yudao.module.ym.controller.admin.site.vo.SitePageReqVO;
import cn.iocoder.yudao.module.ym.util.JsonParserMulti;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ym.controller.admin.ticket.vo.*;
import cn.iocoder.yudao.module.ym.dal.dataobject.ticket.TicketDO;
import cn.iocoder.yudao.module.ym.service.ticket.TicketService;

@Tag(name = "管理后台 - 工单")
@RestController
@RequestMapping("/ym/ticket")
@Validated
public class TicketController {

    @Resource
    private TicketService ticketService;

    @GetMapping("/statistics")
    @Operation(summary = "工单统计/分类统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Map<String,Object>> statistics(@Valid TicketPageReqVO pageReqVO) {
        return success(ticketService.statistics(pageReqVO));
    }

    @PostMapping("/create")
    @Operation(summary = "创建工单")
    public CommonResult<String> createTicket(@Valid @RequestBody TicketSaveReqVO createReqVO) {
        return success(ticketService.createTicket(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单")
    public CommonResult<Boolean> updateTicket(@Valid @RequestBody TicketSaveReqVO updateReqVO) {
        ticketService.updateTicket(updateReqVO);
        return success(true);
    }

    @PutMapping("/edit")
    @Operation(summary = "更新工单")
    public CommonResult<Boolean> editTicket(@Valid @RequestBody TicketSaveReqVO updateReqVO) {
        ticketService.editTicket(updateReqVO);
        return success(true);
    }

    @PutMapping("/redistribute")
    @Operation(summary = "重新派发")
    public CommonResult<Boolean> redistribute(@Valid @RequestBody TicketSaveReqVO updateReqVO) {
        updateReqVO.setStartTime(LocalDateTime.now());
        ticketService.redistribute(updateReqVO);
        return success(true);
    }

    @PutMapping("/refuse")
    @Operation(summary = "拒单")
    public CommonResult<Boolean> refuse(@Valid @RequestBody TicketSaveReqVO updateReqVO) {
        updateReqVO.setStartTime(LocalDateTime.now());
        ticketService.refuse(updateReqVO);
        return success(true);
    }

    @PutMapping("/takeOrders")
    @Operation(summary = "接单")
    public CommonResult<Boolean> takeOrders(@Valid @RequestBody TicketSaveReqVO updateReqVO) {
        updateReqVO.setStartTime(LocalDateTime.now());
        ticketService.takeOrders(updateReqVO);
        return success(true);
    }

    @PutMapping("/overOrders")
    @Operation(summary = "完单")
    public CommonResult<Boolean> overOrders(@Valid @RequestBody TicketSaveReqVO updateReqVO) {
        updateReqVO.setEndTime(LocalDateTime.now());
        ticketService.overOrders(updateReqVO);
        return success(true);
    }

    @PutMapping("/closeOrders")
    @Operation(summary = "闭单")
    public CommonResult<Boolean> closeOrders(@Valid @RequestBody TicketSaveReqVO updateReqVO) {
        ticketService.closeOrders(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteTicket(@RequestParam("id") Long id) {
        ticketService.deleteTicket(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<TicketRespVO> getTicket(@RequestParam("id") Long id) {
        return success(ticketService.getTicket(id));
    }

    @GetMapping("/myCreateList")
    @Operation(summary = "获得我发起的工单不分页")
    public CommonResult<List<TicketRespVO>> getMyCreateLise(@Valid TicketPageReqVO pageReqVO) {
        List<TicketRespVO> pageResult = ticketService.getMyCreateLise(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/myCreatePage")
    @Operation(summary = "获得我发起的工单分页")
    public CommonResult<PageResult<TicketRespVO>> getMyCreatePage(@Valid TicketPageReqVO pageReqVO) {
        PageResult<TicketRespVO> pageResult = ticketService.getMyCreatePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/myList")
    @Operation(summary = "获得派给我的工单不分页")
    public CommonResult<List<TicketRespVO>> getMyLise(@Valid TicketPageReqVO pageReqVO) {
        List<TicketRespVO> pageResult = ticketService.getMyList(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/myPage")
    @Operation(summary = "获得派给我的工单分页")
    public CommonResult<PageResult<TicketRespVO>> getMyPage(@Valid TicketPageReqVO pageReqVO) {
        PageResult<TicketRespVO> pageResult = ticketService.getMyPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单分页")
    public CommonResult<PageResult<TicketRespVO>> getTicketPage(@Valid TicketPageReqVO pageReqVO) {
        PageResult<TicketRespVO> pageResult = ticketService.getTicketPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/list")
    @Operation(summary = "获得工单不分页")
    public CommonResult<List<TicketRespVO>> getTicketList(@Valid TicketPageReqVO pageReqVO) {
        List<TicketRespVO> pageResult = ticketService.getTicketList(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工单 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTicketExcel(@Valid TicketPageReqVO pageReqVO,
              HttpServletResponse response) throws Exception {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TicketRespVO> list = ticketService.getTicketPage(pageReqVO).getList();
        if(list != null && !list.isEmpty()){
            for (TicketRespVO ticketRespVO : list) {
                String inspectionItem = ticketRespVO.getInspectionItem();
                if(inspectionItem != null && !inspectionItem.isEmpty()){
                    String parse = JsonParserMulti.parse(inspectionItem);
                    ticketRespVO.setInspectionItem(parse);
                }
            }
        }
        // 导出 Excel
        ExcelUtils.write(response, "工单.xls", "数据", TicketRespVO.class,
                list);
    }

//    @GetMapping("/pending-ticket")
//    @Operation(summary = "未处理工单统计")
//    public CommonResult<Integer> pendingTicket (SitePageReqVO pageReqVO){
//        return success(ticketService.pendingTicket(pageReqVO));
//    }

    @GetMapping("/getDailyTicket")
    @Operation(summary = "获取某人的每日的订单数量")
    public CommonResult<PageResult<Map<String, Object>>> getDailyTicketCountByOperator(Long operatorId,  Integer pageNo, Integer pageSize   ){

        return success(ticketService.getDailyTicketCountByOperator(operatorId,pageNo,pageSize));
    }

    @GetMapping("/getDailyTicketList")
    @Operation(summary = "获取某人的每日的订单数量不分页")
    public CommonResult<List<Map<String, Object>>> getDailyTicketCountByOperator(Long operatorId){
        return success(ticketService.getDailyTicketCountByOperator(operatorId));
    }


    @GetMapping("/ticketTypeCount")
    @Operation(summary = "获取每日不同类型的工单数量")
    public CommonResult<TicketTypeRespVO> ticketTypeCount(@Param("sTime") LocalDate sTime,@Param("operatorId") Long operatorId){
        return success(ticketService.ticketTypeCount(sTime,operatorId));
    }

}
